/* uedb_param.c */
#include <stdlib.h>
#include <string.h>

/* UELogger Support */
#include "litelog.h"
extern int logfd;

#include "uedb.h"
#include "uedb_param.h"

int addparam(uedb_param **pparamlist, const char* param)
{
	int ret = UE_DB_ERR_SUCCESS;
	uedb_param *tail, *pparam;
	const char *p;

	/* function entrance */
	dbglogex("Enter addparam(%x, %s)", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__, pparamlist, param);

	/* parameter check */
	if (pparamlist)
	{
		if (param && param[0] != 0)
		{
			/* create new query item and fill related fields */
			pparam = (uedb_param*)malloc(sizeof(uedb_param));

			if (pparam)
			{
				memset(pparam, 0, sizeof(uedb_param));

				/* parse the query name */
				p = param;
				while (*p != '=' && *p != 0)
					p++;

				if (*p != 0)
				{
					strncpy_s(pparam->name, MAX_UEDB_PARAM_NAME_LEN + 1, param, __min(MAX_UEDB_PARAM_NAME_LEN, (unsigned int)p - (unsigned int)param));
					strncpy_s(pparam->value, MAX_UEDB_PARAM_VALUE_LEN, ++p, MAX_UEDB_PARAM_VALUE_LEN);
					_strupr_s(pparam->name, MAX_UEDB_PARAM_NAME_LEN + 1);
				}
				else
				{
					strncpy_s(pparam->name, MAX_UEDB_PARAM_NAME_LEN + 1, param, MAX_UEDB_PARAM_NAME_LEN);
					_strupr_s(pparam->name, MAX_UEDB_PARAM_NAME_LEN + 1);
				}
				dbglogex("new parameter name: \"%s\", value: \"%s\"", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__, pparam->name, pparam->value);
				
				/* append the new item into query list */
				tail = *pparamlist;
				if (tail)
				{
					while (tail->next)
						tail = tail->next;

					tail->next = pparam;
				}
				else
				{
					*pparamlist = pparam;
				}
			} /* malloc memory for new parameter item failed */
			else
			{
				dbglog("Malloc memory for new item in parameter list failed", DEBUG_LOG_LEVEL_ERRO, logfd, __FILE__, __LINE__);
				ret = UE_DB_ERR_OUT_OF_MEMORY;
			} /* malloc memory for new parameter item failed */
		} /* input parameter is empty string */
		else
		{
			dbglog("Input parameter is empty string, ignore", DEBUG_LOG_LEVEL_WARN, logfd, __FILE__, __LINE__);
		} /* input parameter is empty string */
	} /* parameter check success */
	else
	{
		dbglog("Invalid parameter, pparamlist is null", DEBUG_LOG_LEVEL_ERRO, logfd, __FILE__, __LINE__);
		ret = UE_DB_ERR_INVALID_PARAMETER;
	} /* parameter check failed */

	dbglog("Exit addparam", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__);

	return ret;
}

const char* getparam(const uedb_param *paramlist, const char* name)
{
	const uedb_param *pparam;
	char buf[MAX_UEDB_PARAM_NAME_LEN + 1];
	const char *value = 0;

	/* function entrance */
	dbglogex("Enter getparam(%x, %s)", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__, paramlist, name);

	/* parameter check */
	if (paramlist && name)
	{
		memset(buf, 0, MAX_UEDB_PARAM_NAME_LEN + 1);
		strcpy_s(buf, MAX_UEDB_PARAM_NAME_LEN + 1, name);

		_strupr_s(buf, MAX_UEDB_PARAM_NAME_LEN + 1);
		pparam = paramlist;
		while (pparam)
		{
			if (strcmp(pparam->name, buf) == 0)
				value = pparam->value;

			pparam = pparam->next;
		}
	} /* parameter check sucess */
	else
	{
		dbglog("Check parameter failed.", DEBUG_LOG_LEVEL_WARN, logfd, __FILE__, __LINE__);
	} /* parameter check failed */

	dbglog("Exit getparam", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__);

	return value;
}

int parseparams(uedb_param **pparamlist, const char* params)
{
	int ret = UE_DB_ERR_SUCCESS;                         /* return code */
	char *p, *q, *buf;
	int len;

	/* function entrance */
	dbglogex("Enter parseparams(%x, %s)", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__, pparamlist, params);

	buf = 0;

	/* parameter check */
	if (pparamlist)
	{
		/* check if there is nay input parameters */
		if (params)
		{
			len = strlen(params);
			buf = (char*)malloc(len + 1);
			if (buf)
			{
				strcpy_s(buf, len + 1, params);
				p = buf;

				/* loop to parse all the paramters */
				while (*p)
				{
					q = p;
					while (*q != '&' && *q != 0)
						q++;

					if (*q == 0)
					{
						ret = addparam(pparamlist, p);

						p = q;
					} /* reach the end of string */
					else
					{
						*q = 0;
						ret = addparam(pparamlist, p);
						*q = '&';

						p = ++q;
					} /* not reach the end of string */

					if (ret != UE_DB_ERR_SUCCESS)
					{
						dbglog("Add new parameter item failed.", DEBUG_LOG_LEVEL_ERRO, logfd, __FILE__, __LINE__);
						break;
					}
				} /* end of loop to parse all the query paramters */

				free(buf);
			} /* malloc memory to store input string success */
			else
			{
				dbglog("Malloc memory to store input string failed.", DEBUG_LOG_LEVEL_ERRO, logfd, __FILE__, __LINE__);
				ret = UE_DB_ERR_OUT_OF_MEMORY;
			} /* malloc memory to store input string failed */
		} /* input parameters string is not null */
	} /* check parameter success */
	else
	{
		dbglog("Invalid parameter, pparamlist is null", DEBUG_LOG_LEVEL_ERRO, logfd, __FILE__, __LINE__);
		ret = UE_DB_ERR_INVALID_PARAMETER;
	} /* check parameter failed */


	dbglog("Exit parseparams", DEBUG_LOG_LEVEL_INFO, logfd, __FILE__, __LINE__);

	return ret;
}